package org.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.exception.ServiceException;
import org.chen.domain.dto.VoiceQueryDTO;
import org.chen.domain.entity.VideoArticle;
import org.chen.domain.entity.VideoAvatar;
import org.chen.domain.entity.VideoVoice;
import org.chen.domain.response.PageResult;
import org.chen.domain.vo.ArticleVO;
import org.chen.domain.vo.VoiceVO;
import org.chen.mapper.VoiceMapper;
import org.chen.service.VoiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class VoiceServiceImpl extends ServiceImpl<VoiceMapper,VideoVoice> implements VoiceService {
    
    @Autowired
    private VoiceMapper voiceMapper;
    
    @Override
    public PageResult<VoiceVO> getVoiceList(VoiceQueryDTO voiceQueryDTO) {
        Page<VideoVoice> page = new Page<>(voiceQueryDTO.getPageNum(), voiceQueryDTO.getPageSize());

        LambdaQueryWrapper<VideoVoice> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(voiceQueryDTO.getLanguage())) {
            wrapper.eq(VideoVoice::getLanguage, voiceQueryDTO.getLanguage());
        }
        
        if (StringUtils.hasText(voiceQueryDTO.getGender())) {
            wrapper.eq(VideoVoice::getGender, voiceQueryDTO.getGender());
        }

        wrapper.orderByAsc(VideoVoice::getLanguage)
               .orderByAsc(VideoVoice::getName);

        Page<VideoVoice> videoVoicePage = voiceMapper.selectPage(page, wrapper);

        List<VoiceVO> list = videoVoicePage.getRecords().stream()
                .map(this::convertToVO)
                .toList();



        return new PageResult<>(list,
                page.getTotal(),
                page.getSize(),
                page.getCurrent());
    }

    @Override
    public Map<String, Map<String, Map<String, Long>>> getVoiceStats() {
        List<VideoVoice> voices = voiceMapper.selectList(null);

        Map<String, Map<String, Map<String, Long>>> stats = new HashMap<>();

        // 按语言和性别分组统计，统一性别字段大小写
        Map<String, Map<String, Long>> languageGenderStats = voices.stream()
                .collect(Collectors.groupingBy(
                        VideoVoice::getLanguage,
                        Collectors.groupingBy(
                                // 统一性别大小写，将大小写不同的 'Male' 和 'male' 处理为统一格式
                                voice -> voice.getGender().toLowerCase(),
                                Collectors.counting()
                        )
                ));

        stats.put("byLanguageAndGender", languageGenderStats);

        return stats;
    }

    @Override
    public VideoVoice getVoiceById(String id) {
        LambdaQueryWrapper<VideoVoice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VideoVoice::getVoiceId, id);
        VideoVoice voice = voiceMapper.selectOne(wrapper);
        if (voice == null) {
            throw new ServiceException("语言数据不存在");
        }
        return voice;
    }

    private VoiceVO convertToVO(VideoVoice voice) {
        VoiceVO vo = new VoiceVO();
        BeanUtils.copyProperties(voice, vo);
        return vo;
    }
}